package com.xayy.health.mgt.service.impl;

import cn.kawins.base.result.CheckResult;
import cn.kawins.base.utils.CollUtil;
import cn.kawins.mybatis.base.BaseServiceImpl;
import com.xayy.health.mgt.model.FaultScoreConfig;
import com.xayy.health.mgt.service.FaultScoreConfigService;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * 故障等级评分配置管理
 * 
 * @author plq
 * @date 2023-05-24
 */
@Service
public class FaultScoreConfigServiceImpl extends BaseServiceImpl<FaultScoreConfig> implements FaultScoreConfigService {
    /**
     * 保存
     *
     * @param faultScoreConfig 对象
     * @return 结果
     */
    @Override
    public CheckResult<?> saveAlgorithmInfo(FaultScoreConfig faultScoreConfig) {
        /*校验频度和程度标识是否重复*/
        List<FaultScoreConfig> selectCode = select(FaultScoreConfig.builder()
            .frequentKey(faultScoreConfig.getFrequentKey()).levelKey(faultScoreConfig.getLevelKey()).build());
        if (CollUtil.isNotEmpty(selectCode)) {
            return CheckResult.fail("频度名称和程度标识重复");
        }
        faultScoreConfig.setId(null);
        insert(faultScoreConfig);
        return CheckResult.success("保存成功");
    }

    /**
     * 根据标识获取故障等级配置信息
     * @param frequentKey 标识
     * @param levelKey 故障等级标识
     * @return 故障等级配置信息集合
     */
    @Override
    public CheckResult<FaultScoreConfig> selectScoreByKey(String frequentKey, String levelKey) {
        FaultScoreConfig faultScoreConfig =
            selectOne(FaultScoreConfig.builder().frequentKey(frequentKey).levelKey(levelKey).build());
        return CheckResult.success(faultScoreConfig);
    }

    /**
     * 获取故障等级列表
     *
     * @return 故障等级列表
     */
    @Override
    public CheckResult<Object> getFaultLevelList() {
        List<FaultScoreConfig> faultScoreConfigs = selectAll();
        TreeSet<FaultScoreConfig> level = faultScoreConfigs.stream()
                .collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(FaultScoreConfig::getLevelName))));
        return CheckResult.success(level);
    }

}
